class Cart

  attr_reader :items

  def initialize
    @items = []
  end


  def add_product(product_id,quantity=1)
    current_item = @items.find {|item| item.product_id == product_id}
    if current_item
      current_item.increment_quantity(quantity)
    else
      current_item = CartItem.new(product_id,quantity)
      @items << current_item
    end
    current_item
  end


  #START:total_items
  def total_items
    @items.sum { |item| item.quantity }
  end
  #END:total_items

  def total_price
    @items.sum { |item| item.price }
  end
end